﻿using B.WMS.InWareHouseManagement;
using B.WMS.InWareHouseManagement;
using B.WMS.WMSEntities.GoodsEntities;
using B.WMS.WMSEntities.GoodsEntities;
using B.WMS.WMSEntities.Snventory;
using B.WMS.WMSEntities.User;
using B.WMS.WMSEntities.Warehousety;
using B.WMS.WMSEntities.WMSUpdateLogs;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace B.WMS.EntityFrameworkCore;

[ReplaceDbContext(typeof(IIdentityDbContext))]
[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName("Default")]
public class WMSDbContext :
    AbpDbContext<WMSDbContext>,
    IIdentityDbContext,
    ITenantManagementDbContext
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */

    #region Entities from the modules

    /* Notice: We only implemented IIdentityDbContext and ITenantManagementDbContext
     * and replaced them for this DbContext. This allows you to perform JOIN
     * queries for the entities of these modules over the repositories easily. You
     * typically don't need that for other modules. But, if you need, you can
     * implement the DbContext interface of the needed module and use ReplaceDbContext
     * attribute just like IIdentityDbContext and ITenantManagementDbContext.
     *
     * More info: Replacing a DbContext of a module ensures that the related module
     * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
     */

    //Identity
    public DbSet<IdentityUser> Users { get; set; }
    public DbSet<IdentityRole> Roles { get; set; }
    public DbSet<IdentityClaimType> ClaimTypes { get; set; }
    public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
    public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
    public DbSet<IdentityLinkUser> LinkUsers { get; set; }
    public DbSet<IdentityUserDelegation> UserDelegations { get; set; }
    public DbSet<IdentitySession> Sessions { get; set; }
    // Tenant Management
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

    
    #endregion
    public DbSet<UserModel> userModels { get; set; }
    public WMSDbContext(DbContextOptions<WMSDbContext> options)
        : base(options)
    {

    }
    //第一步：这里需要把自己定义好的实体写到这里  例如：
    //public DbSet<自己的实体> 起的别名 { get; set; }
    public DbSet<Warehouse> AppWarehouseModel { get; set; }
    public DbSet<Location> WMSLocationModel { get; set; }
    public DbSet<InWareHouseModel> InWareHouseModels { get; set; }
    public DbSet<OutWareHouseModel> OutWareHouseModels { get; set; }
    public DbSet<IOWareHouseRecordModel> IOWareHouseRecordModels { get; set; }
    public DbSet<SalesModel> SalesModels { get; set; }
    public DbSet<SalesReturnModel> SalesReturnModels { get; set; }
    public DbSet<Goods> Goods { get; set; }
    public DbSet<Units> Units { get; set; }
    public DbSet<Goodstype> Goodtypes { get; set; }
    public DbSet<WAllocate> WAllocates { get; set; }
    public DbSet<WVendor> WVendors { get; set; } 
    public DbSet<WMSUpdateLog> wMSUpdateLogs { get; set; } 
    public DbSet<DetailModel> Details { get; set; }
    public DbSet<ReviewModel> Reviews { get; set; }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        /* Include modules to your migration db context */

        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();
        builder.ConfigureIdentity();
        builder.ConfigureOpenIddict();
        builder.ConfigureFeatureManagement();
        builder.ConfigureTenantManagement();

        //第二步：这里需要把自己定义好的实体加上特性  例如：
        builder.Entity<Goods>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Goods", WMSConsts.DbSchema);
            b.ConfigureByConvention(); 
            b.Property(x => x.Goodsname).IsRequired().HasMaxLength(20);
        });
        builder.Entity<WVendor>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "WVendor", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            b.Property(x => x.WVendorname).IsRequired().HasMaxLength(20);
        });
        builder.Entity<WAllocate>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "WAllocate", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            b.Property(x => x.WAllocatecoding).IsRequired().HasMaxLength(20);
        });
        builder.Entity<Units>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Units", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            //下面是为实体中某个字段加非空和最大长度特性
            b.Property(x => x.UnitName).IsRequired().HasMaxLength(20);
        });
        builder.Entity<Goodstype>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Goodstype", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            //下面是为实体中某个字段加非空和最大长度特性
            b.Property(x => x.WGoodstypename).IsRequired().HasMaxLength(20);
        });




        #region flh实体表
        builder.Entity<InWareHouseModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "InWareHouseModel", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            //下面是为实体中某个字段加非空和最大长度特性
            b.Property(x => x.Appid).IsRequired().HasMaxLength(18);
            b.Property(x => x.WarehouseId).IsRequired().HasMaxLength(18);
            b.Property(x => x.LocationNo).IsRequired().HasMaxLength(18);
            b.Property(x => x.Inwarehousetype).IsRequired();
            b.Property(x => x.Inwarehousedate).IsRequired();
            b.Property(x => x.Status).IsRequired();
            b.Property(x => x.Relationordernum).IsRequired();
            b.Property(x => x.Suppilier).IsRequired();
            b.Property(x => x.Contacts).IsRequired();
            b.Property(x => x.Contactinfor).IsRequired();
            b.Property(x => x.Operator).IsRequired();
            b.Property(x => x.Isdeleted).IsRequired();
        });

        builder.Entity<OutWareHouseModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "OutWareHouseModel", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
                                       //下面是为实体中某个字段加非空和最大长度特性
            b.Property(x => x.Appid).IsRequired().HasMaxLength(18);
            b.Property(x => x.Outwarehousetype).IsRequired();
            b.Property(x => x.Warehousename).IsRequired().HasMaxLength(10);
            b.Property(x => x.Outwarehousedate).IsRequired();
            b.Property(x => x.Status).IsRequired();
            b.Property(x => x.RelationGoodsnum).IsRequired().HasMaxLength(18);
            b.Property(x => x.Customercompany).IsRequired().HasMaxLength(20);
            b.Property(x => x.Contacts).IsRequired().HasMaxLength(10);
            b.Property(x => x.Contactinfor).IsRequired().HasMaxLength(11);
            b.Property(x => x.Operator).IsRequired().HasMaxLength(10);
            b.Property(x => x.Isdelete).IsRequired();
        });

        builder.Entity<IOWareHouseRecordModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "IOWareHouseRecordModel", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
                                       //下面是为实体中某个字段加非空和最大长度特性
            b.Property(x => x.Appid).IsRequired().HasMaxLength(18);
            b.Property(x => x.Recordtype).IsRequired();
            b.Property(x => x.Status).IsRequired();
            b.Property(x => x.Operationdate).IsRequired();
            b.Property(x => x.WarehouseId).IsRequired();
            b.Property(x => x.Relationordernum).IsRequired();
            b.Property(x => x.GoodsId).IsRequired();
            b.Property(x => x.GoodsName).IsRequired();
            b.Property(x => x.Operationuser).IsRequired().HasMaxLength(10);
            b.Property(x => x.Num).IsRequired();
            b.Property(x => x.Util).IsRequired().HasMaxLength(18);
            b.Property(x => x.Money).IsRequired();
            b.Property(x => x.Isdelete).IsRequired();
        });

        builder.Entity<SalesModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "SalesModel", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
                                       //下面是为实体中某个字段加非空和最大长度特性
            b.Property(x => x.Appid).IsRequired().HasMaxLength(18);
            b.Property(x => x.Salesdate).IsRequired();
            b.Property(x => x.Goodsappid).IsRequired().HasMaxLength(18);
            b.Property(x => x.Salesgoods).IsRequired().HasMaxLength(10);
            b.Property(x => x.Num).IsRequired();
            b.Property(x => x.Salesmoney).IsRequired();
            b.Property(x => x.Customercompany).IsRequired().HasMaxLength(20);
            b.Property(x => x.Contacts).IsRequired().HasMaxLength(10);
            b.Property(x => x.Contactinfor).IsRequired().HasMaxLength(10);
            b.Property(x => x.Status).IsRequired();
            b.Property(x => x.Isdelete).IsRequired();
        });

        builder.Entity<SalesReturnModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "SalesReturnModel", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
                                       //下面是为实体中某个字段加非空和最大长度特性
            b.Property(x => x.Appid).IsRequired().HasMaxLength(18);
            b.Property(x => x.Salesid).IsRequired().HasMaxLength(18);
            b.Property(x => x.Returnsdate).IsRequired();
            b.Property(x => x.Customercompany).IsRequired().HasMaxLength(10);
            b.Property(x => x.Returnreason).IsRequired().HasMaxLength(100);
            b.Property(x => x.Status).IsRequired();
            b.Property(x => x.Isdelete).IsRequired();
        });

        builder.Entity<WMSUpdateLog>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "WMSUpdateLog", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
                                       //下面是为实体中某个字段加非空和最大长度特性
            b.Property(x => x.Version).IsRequired().HasMaxLength(10);
            b.Property(x => x.Title).IsRequired().HasMaxLength(20);
            b.Property(x => x.Content).IsRequired();
            b.Property(x => x.Files).IsRequired();
            b.Property(x => x.UpdateDate).IsRequired();
        });


        #endregion 




        //第二步：这里需要把自己定义好的实体加上特性  例如：
        

        builder.Entity<UserModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "UserModel", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
        builder.Entity<Goods>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Goods", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
                                       //下面是为实体中某个字段加非空和最大长度特性
            b.Property(x => x.Goodsname).IsRequired().HasMaxLength(20);
        });
        builder.Entity<Units>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Units", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
                                       //下面是为实体中某个字段加非空和最大长度特性
            b.Property(x => x.UnitName).IsRequired().HasMaxLength(20);
        });
        builder.Entity<Goodstype>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Goodstype", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
                                       //下面是为实体中某个字段加非空和最大长度特性
            b.Property(x => x.WGoodstypename).IsRequired().HasMaxLength(20);
        });
        //第三步：去迁移，完成数据库表的创建

        builder.Entity<Location>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Location", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            b.Property(x => x.LocationName).IsRequired().HasMaxLength(128);
        });
        builder.Entity<DetailModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "DetailModel", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            b.Property(x => x.GoodscodeName).IsRequired().HasMaxLength(128);
        });

        builder.Entity<ReviewModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "ReviewModel", WMSConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props
            b.Property(x => x.ReviewNo).IsRequired().HasMaxLength(18);
        });

    }
}
